package com.cdie.myschool.util;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.cdie.myschool.domain.entity.BookHistoryOld;
import com.cdie.myschool.domain.entity.EduReformOld;
import com.cdie.myschool.domain.entity.PapersOld;
import com.cdie.myschool.domain.entity.TeacherOld;
import com.cdie.myschool.mapper.StudentTermReportMapper.BookHistoryOldMapper;
import com.cdie.myschool.mapper.TeacherTermReportMapper.EduReformOldMapper;
import com.cdie.myschool.mapper.TeacherTermReportMapper.PapersOldMapper;
import com.cdie.myschool.mapper.TeacherTermReportMapper.TeacherOldMapper;
import com.cdie.myschool.service.StudentTermReportService;
import com.cdie.myschool.service.TeacherTermReportService;
import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.session.ResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 明祺奇-199000331
 * @date 2022/10/5 - 9:37
 */
@Component
public class DBUtil {
    //每批处理的大小
    private final static int BATCH_SIZE = 1;
    private int size;
    /**
     * 教师表
     * 存储每批数据的临时容器
     * */
    private List<TeacherOld> teacherDataList = new ArrayList<TeacherOld>();
    /**
     * @Description:科研信息表
     * 存储每批数据的临时容器
     */
    private List<PapersOld> papersOldList=new ArrayList<>();
    /**
     * @Description:教改表
     * @Author: 毛康源
     * @Date: 2022/10/9
     */
    private List<EduReformOld> eduReformDataList=new ArrayList<EduReformOld>();
    /**
     * @Description:图书借阅表
     * @Author: 毛康源
     * @Date: 2022/10/12
     */
    private List<BookHistoryOld> bookHistoryOldList=new ArrayList<>();

    @Autowired
    private TeacherOldMapper teacherOldMapper;
    @Autowired
    private PapersOldMapper papersOldMapper;
    @Autowired
    private EduReformOldMapper eduReformOldMapper;
    @Autowired
    private BookHistoryOldMapper bookHistoryOldMapper;

    @Autowired
    private TeacherTermReportService teacherTermReportService;
    @Autowired
    private StudentTermReportService studentTermReportService;


    /**
     * 教师表
     */

    public void getTeacherData(){
        teacherOldMapper.getTeacherData(new ResultHandler<TeacherOld>() {
            @Override
            public void handleResult(ResultContext<? extends TeacherOld> resultContext) {
                TeacherOld teacher = resultContext.getResultObject();
                System.out.println(teacher);
                teacherDataList.add(teacher);
                size++;
                if (size == BATCH_SIZE) {
                    handle();
                }
            }
        });
        return;
    }

    private void handle(){
        try{
            // 在这里可以对你获取到的批量结果数据进行需要的业务处理
            teacherTermReportService.insertTeacherDataIntoTeacher(teacherDataList);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            // 处理完每批数据后后将临时清空
            size = 0;
            teacherDataList.clear();
        }
    }
    public void getPapersData(){
        papersOldMapper.getPapersData(new ResultHandler<PapersOld>() {
            @Override
            public void handleResult(ResultContext<? extends PapersOld> resultContext) {
                PapersOld papers = resultContext.getResultObject();
                System.out.println(papers);
                papersOldList.add(papers);
                size++;
                if (size == BATCH_SIZE) {
                    handlerPaper();
                }
            }
        });
        return;
    }

    private void handlerPaper(){
        try{
            // 在这里可以对你获取到的批量结果数据进行需要的业务处理
            teacherTermReportService.insertPaperOldInPapers(papersOldList);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            // 处理完每批数据后后将临时清空
            size = 0;
            papersOldList.clear();
        }
    }
    public void getEduReformData(){
        eduReformOldMapper.getEduReformData(new ResultHandler<EduReformOld>() {
            @Override
            public void handleResult(ResultContext<? extends EduReformOld> resultContext) {
                EduReformOld eduReform=resultContext.getResultObject();
                System.out.println(eduReform);
                eduReformDataList.add(eduReform);
                size++;
                if (size == BATCH_SIZE) {
                    handleEduform();
                }
            }
        });
    }
    private void handleEduform() {
        try{
            // 在这里可以对你获取到的批量结果数据进行需要的业务处理
            teacherTermReportService.insertEduReformDataInEdureform(eduReformDataList);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            // 处理完每批数据后后将临时清空
            size = 0;
            eduReformDataList.clear();
        }
    }

    public void getBookHistoryData() {
        bookHistoryOldMapper.getBookHistoryData(new ResultHandler<BookHistoryOld>() {
            @Override
            public void handleResult(ResultContext<? extends BookHistoryOld> resultContext) {
                BookHistoryOld bookHistoryOld=resultContext.getResultObject();
                System.out.println(bookHistoryOld);
                bookHistoryOldList.add(bookHistoryOld);
                size++;
                if (size == BATCH_SIZE) {
                    handle3();
                }
            }
        });
    }
    private void handle3() {
        try{
            // 在这里可以对你获取到的批量结果数据进行需要的业务处理
            studentTermReportService.insertBookHistoryDataInBookHistory(bookHistoryOldList);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            // 处理完每批数据后后将临时清空
            size = 0;
            bookHistoryOldList.clear();
        }
    }
}
